function copyNums = getAllCopyNumsRound(tree, leafCopyNums, leafNames)
%UNTITLED Summary of this function goes here
%   Detailed explanation goes here


numLeaves = size(leafCopyNums, 1);
numSites = size(leafCopyNums, 2);
copyNums = zeros(2 * numLeaves - 1, numSites);
[mat, id, dists] = getmatrix(tree);

for i = 1:numLeaves
    for j = 1:numLeaves
        if (strcmp(id(i), leafNames(j)) == 1)
            copyNums(i, :) = leafCopyNums(j, :);
        end
    end
end

numContribs = zeros(2 * numLeaves - 1, 1);
distToChildren = zeros(2 * numLeaves - 1, 1);


curNodes = (1:numLeaves)';
while (size(curNodes, 1) ~= 0)
    fprintf('Have %d nodes\n', size(curNodes, 1));
    nextIdx = 1;
    distIdx = 0;
    nextCurNodes = [];
    for i = 1:size(mat, 2)
        for j = 1:size(mat, 1)
            if (mat(j, i) == 1)
                distIdx = distIdx + 1;
                for k = 1:size(curNodes, 1)
                    %if there is a branch from j->i and i is in curNodes 
                    if (i == curNodes(k, 1))
                        %update j's copy number profile
                        dist = dists(distIdx);
                        if (dist == 0)
                            dist = 10e-5;
                        end
                        dist = 1 / dist;
                        updateVector = getCopyVectorFromTree(tree, copyNums, id(i));
                        copyNums(j, :) = copyNums(j, :) + updateVector(1, :) * dist;
                        numContribs(j, 1) = numContribs(j, 1) + 1;
                        distToChildren(j, 1) = distToChildren(j, 1) + dist;                        
                        if (numContribs(j, 1) == 2)
                            nextCurNodes(nextIdx, 1) = j;
                            nextIdx = nextIdx + 1;
                        end
                    end
                end
            end
        end
    end
    
    curNodes = nextCurNodes;
    % for each node just added, we need to divide by the total distance
    for i = 1:size(curNodes, 1)
        copyNums(curNodes(i, 1), :) = copyNums(curNodes(i, 1), :) / distToChildren(curNodes(i, 1), 1);
    end
    
end

copyNums = round(copyNums);

%for i = numLeaves + 1 : 2 * numLeaves - 1
%    if numContribs(i, 1) ~= 2
%        fprintf('error: got %d contribs for %d\n', numContribs(i, 1), i);
%    end
%end
end

